﻿using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System.ComponentModel.DataAnnotations;
using System.Reflection;

namespace ManagementPlatform.Controllers
{
    /// <summary>
    /// 初始化数据库
    /// </summary>

    public class InitDBTableController : BaseController
    {
        IConfiguration IConfiguration { get; set; }
        public InitDBTableController(IConfiguration IConfiguration)
        {
        this.IConfiguration = IConfiguration;
        }
        [HttpGet]
        public string InitDBTable()
        {
          var strConnt= IConfiguration.GetConnectionString("ConnectionString");
            ConnectionConfig connectionConfig = new ConnectionConfig();
            connectionConfig.ConnectionString = strConnt;
             connectionConfig.DbType = DbType.SqlServer;
            using (SqlSugarClient client = new SqlSugarClient(connectionConfig))
            {
                client.DbMaintenance.CreateDatabase();
                Assembly assembly = Assembly.LoadFile(Path.Combine(AppContext.BaseDirectory, "Models.dll"));
                Type[] typeArray = assembly.GetTypes().Where(it => it.FullName.Contains("Models")).ToArray();
                client.CodeFirst.InitTables(typeArray);
                
               
            }
                return "初始化数据库成功";
        }

    }
}
